/******************************************************************
 * Created by: GOH CHEE HONG                                      
 * Date of Creation: 1/Jan/2009     
 * Date of last modification: 
 *                                                                
 * Description: Form showing the status of the downloading of a offline file

 ******************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Pbdms.Forms.Views
{
    public partial class FrmFileDownloading : Form
    {
        private Common.DataObject.OfflineFile File;
        private delegate void DelegateWorker(object sender, string args);
        private string saveFilePath;
        Thread thDownloadFile;
        BusinessLogic.Mail.FileDownloadManager fileDownloadManager;
        public FrmFileDownloading()
        {
            InitializeComponent();
            Pbdms.Common.Global.paintBackGround(this);
            this.Shown += new EventHandler(FrmFileDownloading_Shown);
            this.FormClosing += new FormClosingEventHandler(FrmFileDownloading_FormClosing);
        }

        void FrmFileDownloading_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (thDownloadFile != null)
            {
                if (fileDownloadManager != null)
                {
                    fileDownloadManager.Cancel();
                }
                thDownloadFile.Abort();

            }
        }

        void FrmFileDownloading_Shown(object sender, EventArgs e)
        {
            fileDownloadManager = new Pbdms.BusinessLogic.Mail.FileDownloadManager(File, saveFilePath,Program.keyControl, Program.publicKeyControl);
            fileDownloadManager.statusProcess += new Pbdms.BusinessLogic.Mail.FileDownloadManager.StatusHandler(setStatus);
            fileDownloadManager.progressProcess += new Pbdms.BusinessLogic.Mail.FileDownloadManager.ProgressHandler(setProgess);
            fileDownloadManager.buttonProcess += new Pbdms.BusinessLogic.Mail.FileDownloadManager.ButtonHandler(setButton);
            fileDownloadManager.VerifierProcess += new Pbdms.BusinessLogic.Mail.FileDownloadManager.VerifierHandler(verifyProcess);
            ThreadStart thsDownloadFile = new ThreadStart(fileDownloadManager.DownloadFile);
            thDownloadFile = new Thread(thsDownloadFile);
            thDownloadFile.IsBackground = true;
            thDownloadFile.Start();
        }
        public void setFileToBeDownload(Common.DataObject.OfflineFile File, string saveFilePath)
        {
            this.File = File;
            this.saveFilePath = saveFilePath;
            this.lblfilename.Text = File.filename;
        }
        private void verifyProcess(object sender, string args)
        {
            if (InvokeRequired)
            {
                DelegateWorker delegateWorker = new DelegateWorker(verifyProcess);
                this.BeginInvoke(delegateWorker, new object[] { sender, args });
            }
            else
            {
                Common.DataObject.Buddy buddy=Program.friendControl.getAFriendByKeyID(args);
                if (buddy != null)
                {
                    this.Text="The file verified to be from "+buddy.Name; 
                }
            }
        }
        /// <summary>
        /// set the status
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void setStatus(object sender, string args)
        {
            if (InvokeRequired)
            {
                DelegateWorker delegateWorker = new DelegateWorker(setStatus);
                this.BeginInvoke(delegateWorker, new object[] { sender, args });
            }
            else
            {

                lblStatus.Text = args;
            }
        }
        /// <summary>
        /// set the progress
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void setProgess(object sender, string args)
        {
            if (InvokeRequired)
            {
                DelegateWorker delegateWorker = new DelegateWorker(setProgess);
                this.BeginInvoke(delegateWorker, new object[] { sender, args });
            }
            else
            {

                progressBar.Value = int.Parse(args);
                
            }
        }
        /// <summary>
        /// set button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void setButton(object sender, string args)
        {
            if (InvokeRequired)
            {
                DelegateWorker delegateWorker = new DelegateWorker(setButton);
                this.BeginInvoke(delegateWorker, new object[] { sender, args });
            }
            else
            {

                btnCancel.Text = "Done";
                progressBar.MarqueeAnimationSpeed = 0;
                progressBar.Maximum = 100;
            }
        }
        /// <summary>
        /// cancel method
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (thDownloadFile != null)
            {
                if (fileDownloadManager != null)
                {
                    fileDownloadManager.Cancel();
                }
                thDownloadFile.Abort();

            }
            this.Close();
        }
        /// <summary>
        /// a method that check whether the download is finished
        /// </summary>
        /// <returns></returns>
        public Boolean finished()
        {
            if (btnCancel.Text.Equals("Done"))
            {
                return true;
            }
            return false;
        }
    }
}